package com.blog.admin.controller;

import com.blog.admin.service.PhotoService;
import com.blog.common.core.domain.R;
import com.blog.common.core.enums.PathEnum;
import com.blog.common.core.web.domain.AjaxResult;
import com.blog.common.domain.dto.PhotoDto;
import com.blog.common.domain.dto.PhotoUploadDto;
import com.blog.system.api.domain.SysFile;
import com.blog.system.api.feign.RemoteFileService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Arrays;

/**
 * <p>
 * 相册图片控制器
 * </p>
 *
 * @author Valerie
 * @date 2023/4/16
 **/
@RestController
@RequiredArgsConstructor
@Slf4j
public class PhotoBackController {

    private final PhotoService photoService;

    private final RemoteFileService remoteFileService;


    /**
     * 查看相册下的全部图片
     *
     * @param albumId 相册id
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/photo/list")
    @ApiOperation("查看相册下的全部图片")
    public AjaxResult listPhotoByAlbumId(@RequestParam("albumId") Serializable albumId){
        return AjaxResult.success(photoService.listPhotoByAlbumId(albumId));
    }

    /**
     * 上传照片
     *
     * @param photoUploadDto 相册id
     *
     * @return {@link AjaxResult}
     */
    @PostMapping("/photo")
    @ApiOperation("上传照片")
    public AjaxResult savePhotoList(@RequestBody PhotoUploadDto photoUploadDto){
        log.info(photoUploadDto.toString());
        photoService.savePhotoList(photoUploadDto);
        return AjaxResult.success();
    }

    /**
     * 删除照片
     *
     * @param jsonNode body
     * @return {@link AjaxResult}
     */

    @DeleteMapping("/photo")
    @ApiOperation("删除照片")
    public AjaxResult removePhoto(@RequestBody JsonNode jsonNode){
        ObjectMapper objectMapper = new ObjectMapper();
        // 获取 "ids" 字段的值
        JsonNode idsNode = jsonNode.get("ids");
        // 将数组转换为 String[]
        Long[] ids = objectMapper.convertValue(idsNode, Long[].class);
        photoService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    /**
     * 修改照片
     *
     * @param photoDto 照片
     * @return {@link AjaxResult}
     */
    @PutMapping("/photo")
    @ApiOperation("修改照片")
    public AjaxResult updatePhoto(@RequestBody PhotoDto photoDto){
        return AjaxResult.success(photoService.updatePhoto(photoDto));
    }


    /**
     * 上传照片
     *
     * @param multipartFile
     * @return {@link AjaxResult}
     */
    @PostMapping("/photo/images")
    @ApiOperation("上传照片")
    @ApiImplicitParam(name = "file", value = "相册图片", required = true, dataType = "MultipartFile")
    public AjaxResult uploadPhoto(@RequestPart("file") MultipartFile multipartFile){
        R<SysFile> sysFileR = remoteFileService.upload(PathEnum.PHOTO, multipartFile);
        return AjaxResult.success(sysFileR.getData());
    }
}
